
import math
import uuid
import sys
sys.path.append("/java_web/cloudcs2mon/pyspace/")
import os
os.chdir(os.path.dirname(__file__))
print("1",os.path.dirname(__file__))

import logs
import confhelper
from playwright.sync_api import Playwright, sync_playwright

import dbmapper
from entity import LinkMonResult, MonLinkBatch, MonLinkVersion, MonLink

logger = logs.get_logger('1688')

wiew_size = {"width": 1380, "height": 2240}
linkpath_prefix = confhelper.confdata().get("linkpath_prefix")+"1688/"
work_dir = confhelper.confdata().get("work_dir")
urls = []
monLinks = []
page_size = 30
picList = []

logger.info("sys.argv: {}".format(sys.argv))
version = sys.argv[1]
def run(playwright: Playwright, link: MonLink) -> None:
    browser = playwright.firefox.launch(headless=False,slow_mo=3000)
    #context = browser.new_context(screen=wiew_size,viewport=wiew_size,storage_state="tb_state.json")
    context = browser.new_context(screen=wiew_size,viewport=wiew_size,storage_state="tb_state.json")
    # Open new page
    page = context.new_page()

    i = 0
    originalUrl = link.url
    url = link.url
    linkId = link.id
    logger.info("开始遍历 url:{} id:{}".format(url,linkId))
    if 'https' not in url:
        url = 'https://' + url
    page.goto(url)
    page.wait_for_load_state('networkidle')

    if page.locator("text=Error 404").count() > 0:
        logger.error("无效店铺: {}".format(url))
        return

    while page.locator("text=Sorry, we have detected unusual traffic from your network.").count() > 0:
        logger.info("出现图形验证等待5秒")
        page.wait_for_timeout(5000)

    if page.locator(".J_giftClose.d-close").count()>0:
        logger.info("出现领券框")
        page.locator(".J_giftClose.d-close").click()

    with page.expect_popup() as page1_info:
        page.get_by_text("全部商品").click()
    page1 = page1_info.value
    page1.wait_for_load_state('networkidle')
    pageTotal = page1.locator("#bd_1_container_0 > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > label:nth-child(1)").inner_text()
    logger.info(pageTotal)

    pageNum = math.ceil(int(pageTotal)/page_size)
    logger.info("url {} pageTotal {} pagenum: {} ".format(url,pageTotal,pageNum))

    monLinkBatch=MonLinkBatch(linkId=linkId)
    linkBatchCount = dbmapper.MonLinkBatchMapper.count(monLinkBatch)
    if int(linkBatchCount) > 0:
        dbmapper.MonLinkBatchMapper.update(MonLinkBatch(goodSum=pageTotal,linkId=linkId))
    else:
        monLinkBatchInsert=MonLinkBatch(goodSum=pageTotal,linkId=linkId,status='0',linkUrl=url)
        dbmapper.MonLinkBatchMapper.insert(monLinkBatchInsert)
    for p_num in range(1,pageNum+1):
        logger.info("进度：url :{} linkId :{} pagenum: {} ".format(url, linkId, pageNum))
        #page.reload();
        if(p_num > 1):
            page1.locator("text=下一页").click()
        pageLinks = page1.locator(".main-picture").all()
        if len(pageLinks) > 0 :
            view_page(p_num,i,page1,pageLinks,picList,url,linkId)

    logger.info("结束遍历 {}".format(url))

    monLinkUpdate=MonLink(version=version, url=originalUrl)
    dbmapper.MonLinkMapper.update(monLinkUpdate)

    page1.close()
    # Close page
    page.close()
    # ---------------------
    context.close()
    browser.close()

def view_page(p_num:int,i:int,page,pageLinks,picList,linkUrl,linkId):
    for p in pageLinks:
        i = i + 1
        j = 0
        try:
            with page.expect_popup(timeout=60000) as popup_info:
                p.first.click()
                logger.info("enabled: {}, visible: {}".format(p.is_enabled(),p.is_visible()))
            page1 = popup_info.value
            page1.wait_for_load_state()

            while page1.locator("text=Sorry, we have detected unusual traffic from your network.").count() >0:
                logger.info("出现图形验证等待5秒")
                page1.wait_for_timeout(5000)
            if page1.locator("text=商品详情").count() > 0 :
                if page1.locator("text=Sorry, we have detected unusual traffic from your network.").count() == 0:
                    pic = linkpath_prefix +str(p_num)+"-"+str(i)+"-"+str(j)+page1.title().replace("*","").replace("/","#").replace(" ","")[0:30]+".png"
                    page1.screenshot(path=pic,full_page=True)
                    picList.append(pic)
                    linkMonResult = LinkMonResult(str(uuid.uuid4().int),linkUrl,page1.url,"/profile"+pic.replace(work_dir,""),linkId=linkId)
                    count = dbmapper.LinkMonResultMapper.count(linkMonResult)
                    if int(count) > 0:
                        linkMonResult.status=0
                        linkMonResult.ocrStr=""
                        dbmapper.LinkMonResultMapper.update(linkMonResult)
                    else:
                        dbmapper.LinkMonResultMapper.insert(linkMonResult)

                    linkVersion=MonLinkVersion(linkId=linkId, linkUrl=linkUrl, goodUrl=page1.url,version=version)
                    lvCount = dbmapper.MonLinkVersionMapper.count(linkVersion);
                    if int(lvCount) <= 0:
                        dbmapper.MonLinkVersionMapper.insert(linkVersion)
                else:
                    logger.info("出现图形验证进入60秒睡眠")
                    page1.wait_for_timeout(60000)

            page1.close()
        except Exception as e:
            logger.info("exception {} url:{}".format(i,page1.url))
            logger.exception("exception traceback is:{}".format(e))
            page1.close()

os.chdir(os.path.dirname(__file__))
# 通过db加载url
monLink = dbmapper.MonLinkMapper.list_by_typea('1688', version)
if len(monLink) > 0:
    monLinks = monLink

logger.info("待监测的url %s",[(link.id,link.url) for link in monLinks])
if len(monLinks) > 0:
    for link in monLinks:
        with sync_playwright() as playwright:
            run(playwright, link)
